#demo seeds file

User.delete_all
Friend.delete_all
Match.delete_all
UsersMatch.delete_all
WatchedStock.delete_all

#Gabe
gabe = User.new(username: 'gabezilla', firstname: 'Gabe', rank: 1,
                email: 'gz@whitehouse.gov', active: Time.now)
gabe.password = '123123password'
gabe.save

#moguls / main friends
moguls = [{username: 'jrock', firstname: 'John'},
          {username: 'carnegie', firstname: 'Andrew'},
          {username: 'star_fighter', firstname: 'timothy'},
          {username: 'crazyeyes', firstname: 'Dale'},
          {username: 'gates', firstname: 'Bill'}]

for i in 0..(moguls.length-1) do
     moguls[i] = User.new(moguls[i].merge(rank: i+2, active: Time.now - 1000*i,
                                          email: "#{moguls[i][:username]}@gmail.com"))
     moguls[i].password = 'asdf'
     moguls[i].save
     Friend.create(user_id: moguls[i].id, friend_id: gabe.id, confirmed: true)
     Friend.create(user_id: gabe.id, friend_id: moguls[i].id, confirmed: true)
end

#Steph
steph = User.new(username: 'wonder_woman', firstname: 'steph', rank: moguls.length+2, 
                 email: 'steph@gmail.com', active: Time.now)
steph.password = 'asdf'
steph.save
Friend.create(user_id: steph.id, friend_id: gabe.id, confirmed: false)

#Tbozz
tbozz = User.new(username: 'tbozz', firstname: 'Tionne', rank: moguls.length+3, 
                 email: 't.b0z@gmail.com', active: Time.now)
tbozz.password = 'asdf'
tbozz.save

# #pending games
# for game in ['Food Fight!', 'Superpowers'] do
#      match = Match.create(owner: gabe.id, title: game, timealloted: 10,
#                           startmoney: 500, begintime: Time.now + 5.days)
#      UsersMatch.create(user_id: gabe.id, match_id: match.id, joined: true, 
#                        money: 500, stocks: {})
# end

# #current games
# for game in ['Office leauge 2013', 'Dino Island'] do
#      match = Match.create(owner: gabe.id, title: game, timealloted: 10,
#                           startmoney: 500, begintime: Time.now - 5.days)
#      UsersMatch.create(user_id: gabe.id, match_id: match.id, joined: true,
#                        money: game.length*777, stocks: {})
# end

# #past games
# for game in ['Everybody loves ActiveRecord'] do
#      match = Match.create(owner: gabe.id, title: game, timealloted: 10,
#                           startmoney: 500, begintime: Time.now - 10.days)
#      UsersMatch.create(user_id: gabe.id, match_id: match.id, joined: true, 
#                        money: game.length*777, stocks: {})
# end


#wheel of doom
wheel = Match.create(owner: gabe.id, title: 'The Strzepek Wheel of Doom', timealloted: 10,
                     startmoney: 500, begintime: Time.now - 5.days)
for mogul in moguls+[gabe] do
     um = UsersMatch.new(user_id: mogul.id, match_id: wheel.id, joined: true, money: 2000)
     um.stocks = { 
          "CAT" => {"price"=>8422, "change"=>"gain", "shares"=>mogul.id / 3}, 
          "CVX" => {"price"=>12114, "change"=>"gain", "shares"=>mogul.id },
          "AAPL" => {"price"=>2424, "change"=>"gain", "shares"=>mogul.id / 2 }
     }
     um.save
end

#old game
old = Match.create(owner: moguls[0].id, title: 'The Game as Old as Time', timealloted: 100,
                   startmoney: 3000, begintime: Time.now - 1000.days())
for mogul in moguls+[gabe] do
     um = UsersMatch.new(user_id: mogul.id, match_id: old.id, joined: true, money: 2000)
     um.stocks = { 
          "GOOG" => {"price"=>13309, "change"=>"loss", "shares"=>mogul.id / 3 },
          "CAT" => {"price"=>8422, "change"=>"gain", "shares"=>mogul.id }, 
          "CVX" => {"price"=>12114, "change"=>"gain", "shares"=>mogul.id },
          "AAPL" => {"price"=>2424, "change"=>"gain", "shares"=>mogul.id / 2 }
     }
     um.save
end

#watched stocks for everyone!
stocks = ["GOOG", "LCC"]
stocks.each do |stock|
     User.all.each do |user|
          watchedStock = WatchedStock.new("symbol"=>stock, "user_id"=>user.id)
          watchedStock.save
     end
end

UsersMatch.all.each do |u|
     u.updateCapital()
end
